package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao implements BaseStudentDao{
    //1.创建学生数组
    private  static Student[] arr= new Student[5];

    static{
        Student stu1 = new Student("heima001","张三",18,"1999-11-11");
        Student stu2 = new Student("heima002","李四",19,"2000-12-12");
        arr[0]=stu1;
        arr[1]=stu2;
    }
    public boolean addStudent(Student stu) {

        //2.添加学生到数组
        //2.1定义变量index为-1,假设数组全部装满,没有null的元素
        int index=-1;
        //2.2遍历数组
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]==null){
                index=i;
                //如果为null,让index变量记录当前索引位置,并使用break结束数组的循环遍历
                break;//此处一定要加break停止,不然会一直往后是null值的位置放.
            }
        }
        //3.判断,返回是否成功的状态
        if (index==-1){
           //装满了
            return false;
        }else{
            //没有装满,正常添加,返回true.
            arr[index]=stu;
            return true;
        }

    }
    //查找学生
    public Student[] findAllStudent() {

            return arr;

    }
    //删除学生
    public boolean deleteStudentById(String delId) {
        boolean flag=false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]!=null&&arr[i].getId().equals(delId)){
                arr[i]=null;
                flag=true;
            }
        }
        return flag;
    }

    //修改学生
    public boolean updateStudent(String updateId, Student newStu) {
        boolean flag=false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]!=null&&arr[i].getId().equals(updateId)){
                arr[i]=newStu;
                flag=true;
            }
        }
        return flag;
    }
}
